package com.linkoog.devtools.http.document.doc_upload.yapi;

import com.intellij.notification.Notification;
import com.intellij.notification.NotificationAction;
import com.intellij.notification.NotificationDisplayType;
import com.intellij.notification.NotificationGroup;
import com.intellij.notification.NotificationType;
import com.intellij.notification.Notifications;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.project.Project;
import com.linkoog.devtools.http.raw_index.HttpServiceMethod;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;

public class YapiDocUploadAction extends AnAction {

    private static NotificationGroup notificationGroup;
    static {
        notificationGroup = new NotificationGroup("DevTools.HttpTest.Yapi",  NotificationDisplayType.BALLOON, true);
    }

    private final HttpServiceMethod httpServiceMethod;

    public YapiDocUploadAction(@NotNull HttpServiceMethod httpServiceMethod, @NotNull String text, String description, Icon icon) {
        super(text, description, icon);
        this.httpServiceMethod = httpServiceMethod;
    }

    @Override
    public void actionPerformed(@NotNull AnActionEvent e) {
        Project project = e.getData(CommonDataKeys.PROJECT);
        if (project == null){
            return;
        }

       /* YapiConfig yapiConfig = TestDevToolsPersistent.getInstance(project).getYapiConfig();
        String host = yapiConfig.getHost();
        if (StringUtils.isBlank(host)){
            showErrorAndSetting(project,"上传Yapi平台失败:<br/>" + "host未设置！", true);
            return;
        }

        String token = yapiConfig.getToken();
        if (StringUtils.isBlank(token)){
            showErrorAndSetting(project,"上传Yapi平台失败:<br/>" + "token未设置！", true);
            return;
        }


        YapiHttp yapiHttp = new YapiHttp(host, token);
        YApiProject yApiProject = null;
        try {
            YApiResponse<YApiProject> response = yapiHttp.getProjectInfo();
            Assert.isTrue(response.getErrcode() == 0, response.getErrmsg());
            yApiProject = response.getData();
        }catch (Throwable throwable){
            showErrorAndSetting(project,"上传Yapi平台失败:<br/>"
                    + "无法获取项目信息，请检查配置！<br/>" + throwable.getMessage(), true);
            return;
        }

        YapiDocUploadDialog uploadDialog = new YapiDocUploadDialog(httpServiceMethod, project, yapiHttp, yApiProject);
        uploadDialog.showAndGet();*/
    }

    private void showErrorAndSetting(Project project, String errorMsg, boolean showSetting) {
        Notification error = notificationGroup.createNotification(errorMsg, NotificationType.INFORMATION);
        if (showSetting){
            error.addAction(new NotificationAction("Yapi Settings") {
                @Override
                public void actionPerformed (@NotNull AnActionEvent anActionEvent, @NotNull Notification notification){
                    /*HttpTestSettingDialog dialog = new HttpTestSettingDialog(project);
                    dialog.showYapiTab();
                    dialog.show();*/
                }
            });
        }
        Notifications.Bus.notifyAndHide(error, project);
    }

    

}
